﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System;
using System.IO;
using System.Text;
using Smartkernel.Attributes;
using Smartkernel.Configuration;
using Smartkernel.IO;
using Smartkernel.Xml;

namespace Smartkernel.Net
{
    /// <summary>
    /// Ftp服务器配置实体，从配置文件中获得配置信息
    /// </summary>
    public partial class SmartFtpClientEntity
    {
        #region 公有属性
        private string ftpServer;
        /// <summary>
        /// Ftp服务器地址
        /// </summary>
        [SmartXmlMappingAttribute("FtpServer")]
        public string FtpServer
        {
            get
            {
                return this.ftpServer;
            }
            set
            {
                this.ftpServer = value;
            }
        }
        private string port;
        /// <summary>
        /// Ftp服务器端口
        /// </summary>
        [SmartXmlMappingAttribute("Port")]
        public string Port
        {
            get
            {
                return this.port;
            }
            set
            {
                this.port = value;
            }
        }
        private string userName;
        /// <summary>
        /// 登录Ftp服务器的用户名
        /// </summary>
        [SmartXmlMappingAttribute("UserName")]
        public string UserName
        {
            get
            {
                return this.userName;
            }
            set
            {
                this.userName = value;
            }
        }
        private string password;
        /// <summary>
        /// 登录Ftp服务器的密码
        /// </summary>
        [SmartXmlMappingAttribute("Password")]
        public string Password
        {
            get
            {
                return this.password;
            }
            set
            {
                this.password = value;
            }
        }
        private static SmartFtpClientEntity instance;
        /// <summary>
        /// 通过配置文件初始化的默认实例
        /// </summary>
        public static SmartFtpClientEntity Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion
    }
    public partial class SmartFtpClientEntity
    {
        #region 构造函数及字段
        /// <summary>
        /// 公有构造函数
        /// </summary>
        public SmartFtpClientEntity()
        {
    
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ftpServer">Ftp服务器地址</param>
        /// <param name="port">Ftp服务器端口</param>
        /// <param name="userName">登录Ftp服务器的用户名</param>
        /// <param name="password">登录Ftp服务器的密码</param>
        public SmartFtpClientEntity(string ftpServer, string port, string userName, string password)
        {
            this.ftpServer = ftpServer;
            this.port = port;
            this.userName = userName;
            this.password = password;
        }
        private static string xmlPath;
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static SmartFtpClientEntity()
        {
            if (SmartAction.IsInvokeFail(delegate { xmlPath = SmartConfiger.GetAppSettingValue<string>("ConfigFolder", true).TrimEnd(new char[] { '\\' }) + @"\SmartFtpClientEntity.config"; }))
            {
                xmlPath = Environment.CurrentDirectory + @"\SmartFtpClientEntity.config";
                if (!File.Exists(xmlPath))
                {
                    try
                    {
                        xmlPath = System.Web.HttpRuntime.AppDomainAppPath.TrimEnd('\\') + @"\ConfigFolder\SmartFtpClientEntity.config";
                    }
                    catch (Exception)
                    { 
                    
                    }
                }
            }
            //存在配置文件的时候才监视文件的变化
            if (File.Exists(xmlPath))
            {
                instance = new SmartFtpClientEntity();
                SmartXml.MappingXO<SmartFtpClientEntity>(xmlPath, SmartMappingType.Attribute, ref instance);
                SmartFileSystemWatcher.Add(xmlPath, delegate { SmartXml.MappingXO<SmartFtpClientEntity>(xmlPath, SmartMappingType.Attribute, ref instance); });
            }
        }
        #endregion
    }
    public partial class SmartFtpClientEntity
    {
        #region 覆盖基类的方法
        /// <summary>
        /// 比较两个对象是否相等
        /// </summary>
        /// <param name="obj">要比较的对象</param>
        /// <returns>比较的结果</returns>
        public override bool Equals(object obj)
        {
            SmartFtpClientEntity other = obj as SmartFtpClientEntity;
            if (other == null)
            {
                return false;
            }
            else
            {
                return this.FtpServer == other.FtpServer && this.Port == other.Port && this.UserName == other.UserName && this.Password == other.Password;
            }
        }
        /// <summary>
        /// 获得哈希码
        /// </summary>
        /// <returns>哈希码</returns>
        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }
        /// <summary>
        /// 获得字符串表示
        /// </summary>
        /// <returns>输出的字符串</returns>
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder(50);
            stringBuilder.Append("SmartFtpClientEntity");
            if (this.FtpServer != null)
            {
                stringBuilder.Append(this.FtpServer.GetHashCode().ToString());
            }
            stringBuilder.Append("__");
            if (this.Port != null)
            {
                stringBuilder.Append(this.Port.GetHashCode().ToString());
            }
            stringBuilder.Append("__");
            if (this.UserName != null)
            {
                stringBuilder.Append(this.UserName.GetHashCode().ToString());
            }
            stringBuilder.Append("__");
            if (this.Password != null)
            {
                stringBuilder.Append(this.Password.GetHashCode().ToString());
            }
            return stringBuilder.ToString();
        }
        #endregion
    }
}
